<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$company = $db->getCell("select `name` from `companies` where `company_id` = '$cid' limit 1");
if (!$company)
	goToPage("/admin/offices");

require_once ADMIN.DS."/head.php";
?>
<h1>Доп. офисы «<?php echo $company; ?>»</h1><?php
	if ($route->GET("add") == "success"){
		echo "<p class='success'><br />Успешно добавлено</p>";
	} elseif ($route->GET("add") == "fail") {
		echo "<p class='fail'><br />Не удалось добавить</p>";
	}
	if ($route->GET("edit") == "success"){
		echo "<p class='success'><br />Успешно обновлено</p>";
	} elseif ($route->GET("edit") == "fail") {
		echo "<p class='fail'><br />Не удалось обновить</p>";
	}
?><br />
<table cellpadding='0' cellspacing='0' class='list'>
	<tr class='table_header'>
		<td width='50px'>ID</td>
		<td width='310px'>Адрес</td>
		<td width='120px'>Телефон</td>
		<td width='200px' class='actions'>Действия</td>
	</tr><?php
		$offices = $db->getTable("select * from `additional_office` where `company_id` = '$cid' order by `ao_id`");
		foreach ($offices as $office){
			echo "<tr><td>$office[ao_id]</td>";
			echo "<td>";
				echo "<form id='form_{$office['ao_id']}' action='/admin/offices/edit/{$office['ao_id']}?cid=$cid' method='post'>";
					echo "<input type='hidden' id='phone_{$office['ao_id']}' name='phone' value='$office[phone]' />";
					echo "<input type='text' style='width: 300px;' name='address' value='$office[address]' />";
				echo "</form>";
			echo "</td>";
			echo "<td><input type='text' style='width: 110px;' value='$office[phone]' onkeyup=\"$('#phone_$office[ao_id]').val($(this).val())\" /></td>";
			echo "<td class='actions'><a href='javascript:void(0)' onclick=\"$('#form_$office[ao_id]').submit()\" class='edit'>Редактировать</a> | <a href='/admin/offices/delete/$office[ao_id]?cid=$cid' class='delete'>Удалить</a></td></tr>";
		}
	?>
	<tr>
		<td></td>
		<td colspan='2'>
			<form id="new_form" action='/admin/offices/add?cid=<?php echo $cid; ?>' method='post'>
				<input type='text' name='address' style='width:300px' />
				<input type='text' name='phone' style="margin-left: 2px; width:110px" />
			</form>
		</td>
		<td><a href='javascript:void(0)' onclick="$('#new_form').submit()" style="margin-left: 25px">Добавить</a></td>
	</tr>
</table>

<?php require_once ADMIN.DS."/footer.php"; ?>